package bb.lanxing.guide;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import bb.lanxing.guide.device.FGHomeGuideComponent;
import bb.lanxing.guide.device.GuideBuilder;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.util.DensityUtil;

public class GuideHelper {
    public static void showGuideIfNeed(AppCompatActivity appCompatActivity, Class<? extends Fragment> cls, int i) {
        if (appCompatActivity == null || cls == null) {
            return;
        }
        Fragment fragment = null;
        try {
            fragment = cls.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e2) {
            e2.printStackTrace();
        }
        if (fragment == null) {
            return;
        }
        String key = getKey(cls);
        if (!SharedManager.getInstance().getBoolean(key, true)) {
            return;
        }
        if (appCompatActivity.findViewById(i) instanceof ViewGroup) {
            ((ViewGroup) appCompatActivity.findViewById(i)).removeAllViews();
        }
        try {
            appCompatActivity.getSupportFragmentManager().beginTransaction().add(i, fragment, key).commit();
        } catch (IllegalStateException e3) {
            e3.printStackTrace();
        }
    }

    public static String getKey(Class<?> cls) {
        return "guide_" + cls.getName();
    }

    public static void finish(Class<?> cls) {
        SharedManager.getInstance().setValue(getKey(cls), false);
    }

    public static boolean shouldShowGuideView(Class<?> cls) {
        return SharedManager.getInstance().getBoolean(getKey(cls), true);
    }

    public static void showFGDeviceHomeGuide(Activity activity, View view, GuideBuilder.OnVisibilityChangedListener onVisibilityChangedListener) {
        GuideBuilder guideBuilder = new GuideBuilder();
        guideBuilder.setTargetView(view).setAlpha(150).setAutoDismiss(true).setOverlayTarget(false).setHighTargetPaddingLeft(DensityUtil.dp2px(2.0f)).setHighTargetPaddingRight(DensityUtil.dp2px(2.0f)).setHighTargetCorner(DensityUtil.dp2px(5.0f)).setHighTargetGraphStyle(0);
        guideBuilder.setOnVisibilityChangedListener(onVisibilityChangedListener);
        guideBuilder.addComponent(new FGHomeGuideComponent());
        guideBuilder.createGuide().show(activity);
    }
}
